﻿using CPPEI.ShareScreen.App.RemoteDesk;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.ShareScreen.App
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private CPPEI.ShareScreen.ServerProxy.ServerProxyClient _proxy;
        UploadDeskThread _uploadDeskThread;
        WindowInteropHelper _helper;

        ScreenCapture2 _screenCapture;

        public MainWindow()
        {
            InitializeComponent();

            _helper = new WindowInteropHelper(this);

            _proxy = new ServerProxy.ServerProxyClient(App.TCPClient);
            CPPEI.Event.NCL.NCLEventBus.Register(_proxy);
            CPPEI.Event.NCL.NCLEventBus.Register(this);

            Event.UI.UIEventBus.Register(this);

            if (App.IsRunningInBackground)
            {
                //this.Visibility = System.Windows.Visibility.Hidden;
            }

            _screenCapture = new ScreenCapture2();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            App.TCPClient.DisConnect();
            base.OnClosing(e);
        }

        private void _ctlBtnConnectToServer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                App.TCPClient.Connect(XEnvironment.Instance.Host.IP, XEnvironment.Instance.Host.Port);
                _ctlBtnConnectToServer.Content = "disconnect from server";
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("连接服务器失败,{0}", ex), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void _ctlRefreshClientList_Click(object sender, RoutedEventArgs e)
        {
            CPPEI.ShareScreen.Event.NCL.NCLGetClientListEvent @event = new Event.NCL.NCLGetClientListEvent();
            CPPEI.Event.NCL.NCLEventBus.PublishAsync(@event);
        }

        private void _ctlPrintScreen_Click(object sender, RoutedEventArgs e)
        {
        }

        private void _ctlConnectToClient_Click(object sender, RoutedEventArgs e)
        {
            ClientInfo selClient = this._ctlListClient.SelectedItem as ClientInfo;
            if (selClient != null)
            {
                CPPEI.ShareScreen.Event.NCL.NCLRDConnectToClientEvent @event = new Event.NCL.NCLRDConnectToClientEvent();
                @event.TargetClientID = (uint)selClient.Id;
                CPPEI.Event.NCL.NCLEventBus.PublishAsync(@event);
            }
        }

        //byte[] 转换 Bitmap
        //public static Bitmap BytesToBitmap(byte[] Bytes)
        //{
        //    MemoryStream stream = null;
        //    try
        //    {
        //        stream = new MemoryStream(Bytes);
        //        return new Bitmap((Image)new Bitmap(stream));
        //    }
        //    catch (ArgumentNullException ex)
        //    {
        //        throw ex;
        //    }
        //    catch (ArgumentException ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        stream.Close();
        //    }
        //}
    }
}
